#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "collector.h"
#include "view.h"

int main(int argc, char **argv)
{
	int opt;
    pid_t pid = -1;
    int verbose = 0;

    // 解析命令行参数
    while ((opt = getopt(argc, argv, "p:v")) != -1) {
        switch (opt) {
            case 'p':
                pid = atoi(optarg);  // 将字符串转换为整数
                break;
            case 'v':
                verbose = 1;         // 标志位设为true
                break;
            default: /* '?' */
                fprintf(stderr, "Usage: %s [-p pid] [-v]\n", argv[0]);
                exit(EXIT_FAILURE);
        }
    }

	Collector collector;
	if (verbose) {
		collector.EnableVerbose();
		EnableVerbose();
	}
	collector.Start(pid);
	start_view();
	
	while (1) {
		sleep(1);
		auto stat = collector.Read();
		render(stat);

		if (check_input() < 0) {
			break;
		}
	}

	end_view();

	return 0;
}
